$(document).ready(function(){           // 创建车辆动态表格
    $("i[id^='zk']").click(function(){
        var i = $(this).attr('id').split("_")[1];
        $("#tv_"+i).hide();
        $("#sq_"+i).show();
        $("#zk_"+i).hide();
    });
    $("i[id^='sq']").click(function(){
        var i = $(this).attr('id').split("_")[1];
        $("#tv_"+i).show();
        $("#sq_"+i).hide();
        $("#zk_"+i).show();
    });
});


function selectConfigure(url,aasa){
    $.ajax({
        type : "post",
        url : 'selectCarConfigure',
        data : {},
        dataType : "json",
        success : function(result) { //弹窗放数据
            if(result.code == 1) {
                alert(result.msg);
                return false;
            }
            var count = 0;
            for (var i = 0 ;i < result.data.length; i++){
                var flag = result.data[i].id+"_"+result.data[i].name+"_"+url+result.data[i].photo;
                if (i % 2 == 0 || i == 0){
                    conut  = i;
                    var tr = "<tr id=tr_0"+i+"></tr>";
                    $("#tbody_01").append(tr);
                }
                var td =  "<td><input id='check_"+ i + "' type='checkbox' name='like' lay-skin=\"primary\"  value="+ flag+"></td>\n" +
                    "<td>" + result.data[i].name + "</td>\n" +
                    "<td><img src='"+ url+result.data[i].photo  +"' width=\"60\"></td>\n" ;
                $("#tr_0"+conut).append(td);

                if (aasa != null){          //编辑页面设置回显选中
                    $.each(aasa,function(x ,t){
                        if (flag == t){
                            $("#check_"+i).prop("checked", true);
                        }
                    })
                }
            }

            if (result.data.length &2 != 0){        //填补空位 奇数
                var td1 = "<td></td><td></td><td></td>";
                $("#tr_0"+conut).append(td1);
            }
            layui.form.render();
        }
    });
}

//新增配置集合 。
var configureList  = new Array();

//创建车辆
$('#add01').on('click',function(){
    layer.open({
        type: 1,
        title: '更多',
        area: ['900px', '680px'],
        btn: ['确定', '取消'],
        content: $('#tan_81'),
        yes:function(index,layero){
            configureList = [];
            reset();
            var count = 0;
            var obj = $("input[type='checkbox'][name='like']:checked");
            $.each(obj,function (index,item){
                configureList.push(item.value);
                var id = item.value.split("_")[0];
                var name = item.value.split("_")[1];
                var url = item.value.split("_")[2];
                if (index % 2 == 0 || index == 0){
                    conut  = index;
                    var tr = "<tr id=tr_0_"+index+"></tr>";
                    $("#tbody_02").before(tr);//prepend
                }
                var param = "<td id=tr_d_"+index+"><a class=\"fa fa-minus-circle\" id='td-a_"+ index+"'  onclick='deletefun(\""+id+"\",\""+url+"\",\""+ name +"\" )' flag="+id+"_"+name+"_"+url+"></a></td>\n" +
                    "            <td>"+ name +"</td>\n" +
                    "            <td><img src='"+  url  +"' width=\"60\"></td>" ;
                $("#tr_0_"+conut).append(param)
            })

            if (obj.length &2 != 0){        //填补空位 奇数
                var td1 = "<td></td><td></td><td></td>";
                $("#tr_0_"+conut).append(td1);
            }
            layui.form.render();
            layer.close(index);
        }
    });
});
//删除
function deletefun (a,b,c){
    reset();
    var flag = a+"_"+c+"_"+b;
    configureList.splice($.inArray(flag,configureList),1);//删除当前数据
    for (var i = 0 ;i < configureList.length; i++ ){
        var id = configureList[i].split("_")[0];
        var name = configureList[i].split("_")[1];
        var url = configureList[i].split("_")[2];
        if (i % 2 == 0 || i == 0){
            conut  = i;
            var tr = "<tr id=tr_0_"+i+"></tr>";
            $("#tbody_02").before(tr);
        }
        var param = "<td><a class=\"fa fa-minus-circle\" id='td-a_"+ i+"' flag="+id+"_"+name+"_"+url+" onclick='deletefun(\""+id+"\",\""+url+"\",\""+ name +"\")'></a></td>\n" +
            "            <td>"+ name +"</td>\n" +
            "            <td><img src='"+  url  +"' width=\"60\"></td>" ;
        $("#tr_0_"+conut).append(param)
    }

    if (configureList.length &2 != 0){        //填补空位 奇数
        var td1 = "<td></td><td></td><td></td>";
        $("#tr_0_"+conut).append(td1);
    }
    //取消选中
    var obj = $("input[type='checkbox'][name='like']:checked");
    $.each(obj,function (index,item){
        if (item.value == flag) {
            $(this).prop("checked", false);
        }
    })
    layui.form.render();
}

//编辑页面回显 以及选中
function updateConfigure(configure,httpurl) {
    for (var i = 0 ;i < configure.length; i++ ){
        var id = configure[i].id;
        var name = configure[i].name;
        var url = httpurl+configure[i].photo;
        var flag =  id+"_"+name+"_"+url;
        configureList.push(flag);
        if (i % 2 == 0 || i == 0){
            conut  = i;
            var tr = "<tr id=tr_0_"+i+"></tr>";
            $("#tbody_02").before(tr);
        }
        var param = "<td><a class=\"fa fa-minus-circle\" id='td-a_"+ i+"' flag="+id+"_"+name+"_"+url+" onclick='deletefun(\""+id+"\",\""+url+"\",\""+ name +"\")'></a></td>\n" +
            "            <td>"+ name +"</td>\n" +
            "            <td><img src='"+  url  +"' width=\"60\"></td>" ;
        $("#tr_0_"+conut).append(param)

    }
    if (configure.length &2 != 0){        //填补空位 奇数
        var td1 = "<td></td><td></td><td></td>";
        $("#tr_0_"+conut).append(td1);
    }
    selectConfigure(httpurl,configureList);//传入数据 设置选中
    layui.form.render();
}


//重置
function reset(){
    $("tr[id^='tr_0_']").each(function(){
        $(this).remove();
    })
}